package com.example.demo.service;

import com.example.demo.mapper.CoffeeMapper;
import com.example.demo.model.Coffee;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.ExampleMatcher;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;

@Slf4j
@Service
public class CoffeeService {
    private static final String CACHE = "springbucks-coffee";
    @Autowired
    private CoffeeMapper coffeeMapper;
//    @Autowired
//    private RedisTemplate<String, Coffee> redisTemplate;

    public Coffee findOneCoffee(String name) {
//        HashOperations<String, String, Coffee> hashOperations = redisTemplate.opsForHash();
//        if (redisTemplate.hasKey(CACHE) && hashOperations.hasKey(CACHE, name)) {
//            log.info("Get coffee {} from Redis.", name);
//            return hashOperations.get(CACHE, name);
//        }
//        ExampleMatcher matcher = ExampleMatcher.matching()
//                .withMatcher("name", exact().ignoreCase());
//
        Coffee  coffee = coffeeMapper.findByName(name);
//        log.info("Coffee Found from db: {}", coffee);
//        if (coffee!=null) {
//            log.info("Put coffee {} to Redis.", name);
//            hashOperations.put(CACHE, name, coffee);
//            redisTemplate.expire(CACHE, 1, TimeUnit.MINUTES);
//        }
        return coffee;
    }
    public Coffee findById(Long id) {
        Coffee  coffee = coffeeMapper.findById(id);
        return coffee;
    }

    public ArrayList<Coffee> findAll() {
        ArrayList<Coffee>  coffeelist = coffeeMapper.findAllCoffee();
        return coffeelist;
    }
}
